
#ifndef FIBER_ENV_COOPERATIVE_CONDITION_H_
#define FIBER_ENV_COOPERATIVE_CONDITION_H_

#include "condition.h"
#include "cooperative-mutex.h"
#include "fiber-env.h"

namespace dulymoo {

class CooperativeCondition : public ICondition
{
public:
    explicit CooperativeCondition(CooperativeMutex &mutex);
    virtual ~CooperativeCondition();

private:
    CooperativeCondition(const CooperativeCondition&);  // unimplemented
    CooperativeCondition& operator= (const CooperativeCondition&);  // unimplemented

public:
    virtual bool Acquire();
    virtual bool TryAcquire();
    virtual void Release();

    virtual bool Wait();
    virtual bool TimedWait(const ABSTime &time);
    virtual bool TimedWait(unsigned int timeout);

    virtual bool Signal();
    virtual bool Broadcast();

private:
    static void CompleteWait(FiberEnv::ThreadContext*, void*);

private:
    CooperativeMutex &mutex_;
    std::deque<FiberContext*> waiting_queue_;
};

}  // namespace dulymoo

#endif  // FIBER_ENV_COOPERATIVE_CONDITION_H_

